import http from '@/utils/http'
import type { User } from './types'

// 定义请求公共请求前缀
const api_name = '/admin/acl/user'

/**
 * 获取用户分页列表
 */
export function getUserList(page: number, limit: number, username?: string) {
  return http.get<User.GetUserListData>(`${api_name}/${page}/${limit}`, {
    username,
  })
}

/**
 * 添加用户
 */
export function addUser(data: User.AddUserParams) {
  return http.post<null>(`${api_name}/save`, data)
}

/**
 * 修改用户
 */
export function updateUser(data: User.AddUserParams) {
  return http.put<null>(`${api_name}/update`, data)
}

/**
 * 获取用户的角色
 */
export function getUserRole(id: number) {
  return http.get<User.GetUserRoleData>(`${api_name}/toAssign/${id}`)
}

/**
 * 给用户分配角色
 */
export function assignUserRole(adminId: number, roleId: number[]) {
  // axios 发生请求的参数类型是 content-type: application/json
  // 但是当前接口需要请求的参数类型是 content-type: application/x-www-form-urlencoded
  // 表单参数格式就是查询字符串，少个问号
  return http.post<null>(
    `${api_name}/doAssign`,
    `adminId=${adminId}&roleId=${roleId.join(',')}`,
    {
      // 请求头
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    },
  )
}

/**
 * 批量删除用户
 */
export function batchRemoveUserList(idList: number[]) {
  return http.delete<null>(`${api_name}/batchRemove`, idList)
}
